package packethandler;

import io.ClientSession;
import io.OperationResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.Platform;
import player.Player;
import protocol.OpCode;
import protocol.ParameterType;
import activity.ActivityBoss;
import activity.ActivityFirst;
import activity.ActivityGrowthWelfare;
import activity.DefenceInstance;

public class ActivityPacketHandler extends PhotonHandlerBase{
protected static final Logger log = LoggerFactory.getLogger(ActivityPacketHandler.class);
	
	protected int[] opcodes = { 
			OpCode.JoinActivity,
			OpCode.FinishActivity,
			OpCode.ActivityBossDamage,
			OpCode.DefenceBattleStart,
			OpCode.BallApplyDamage,
			OpCode.ActivityFirstCharge,
			OpCode.GrowthWelfareInfo,
			OpCode.GetGrowthWelfare,
	};
	
	public ActivityPacketHandler() {
		HandlerDispatchManager.register(HandlerDispatch.PLAYER, opcodes, this);
	}

	@Override
	protected void OnOperationResponse(Object obj, ClientSession session) {
		OperationResponse operationResponse =  (OperationResponse)obj;
		switch(operationResponse.OperationCode){
		case OpCode.JoinActivity:
			JoinActivity(operationResponse, session);
			break;
		case OpCode.FinishActivity:
			FinishActivity(operationResponse, session);
			break;
		case OpCode.ActivityBossDamage:
			activityBossAddDamage(operationResponse, session);
			break;
		case OpCode.DefenceBattleStart:
			defanceBattleStart(operationResponse, session);
			break;
		case OpCode.BallApplyDamage:
			ballApplyDamage(operationResponse, session);
			break;
		case OpCode.ActivityFirstCharge:
			ActivityFirstCharge(operationResponse, session);
			break;
		case OpCode.GrowthWelfareInfo:
			ActivityGrowthWelfare.GetGrowthWelfareInfo(session);
			break;
		case OpCode.GetGrowthWelfare:
			ActivityGrowthWelfare.GetGrowthWelfare(operationResponse, session);
			break;
		}
	}
	
	private void ActivityFirstCharge(OperationResponse operationResponse,
			ClientSession session) {
		Player player = (Player)session.getClient();
		ActivityFirst.getRewards(player);
	}

	private void ballApplyDamage(OperationResponse operationResponse,
			ClientSession session) {
		int damage = (Integer)operationResponse.Parameters.get(ParameterType.hitValue);
		Player p = (Player)session.getClient();
        if(p.getDefenceInstance() != null)
        {
        	DefenceInstance defence = p.getDefenceInstance();
        	defence.addDamage(damage);
        }
		
	}

	private void defanceBattleStart(OperationResponse operationResponse,
			ClientSession session) {
		Player p = (Player)session.getClient();
		int hp  = (Integer)operationResponse.Parameters.get((short)0);
		if(p.getDefenceInstance() != null)
		{
			DefenceInstance defence = p.getDefenceInstance();
			defence.initLightBall(p,hp);
		}
		
	}

	private void activityBossAddDamage(OperationResponse operationResponse,
			ClientSession session) {
		int damage = (Integer)operationResponse.Parameters.get(ParameterType.hitValue);
		Player p = (Player)session.getClient();
        if(p.getActivityInstance() != null)
        {
        	ActivityBoss activity = (ActivityBoss)p.getActivityInstance();
        	activity.addDamage(p,damage);
        }
		
	}

	private void FinishActivity(OperationResponse operationResponse,
			ClientSession session) {
		Player p = (Player)session.getClient();
		String activityID = (String)operationResponse.Parameters.get(ParameterType.ActivityID);
        p.log.info("-----------------------FinishActivity----------------------" + activityID);
        Platform.getActivityManager().FinishActivity(activityID, p, operationResponse);
		
	}

	private void JoinActivity(OperationResponse operationResponse,ClientSession session) {
		Player p = (Player)session.getClient();
		String activityID = (String)operationResponse.Parameters.get(ParameterType.ActivityID);
        p.log.info("-----------------------JoinActivity----------------------" + activityID);  
        Platform.getActivityManager().JoinActivity(activityID, p, operationResponse);	
	}
}
